package com.cr.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cr.model.MyOrder;
import com.cr.util.DBUtil;

/**
 * 
 * @author Administrator
 *
 */
public class MyOrderDao {
	private DBUtil db=new DBUtil();
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	public MyOrder getMyOrder(long id)
	{
		MyOrder myOrder=null;
		
		String sql="select * from myorder where Id=?";
		Object[] args={id};
		ResultSet rs=db.preQuery(sql, args);
		
		if(rs!=null)
		{
			try {
				if(rs.next())
				{
					myOrder=new MyOrder();
					
					myOrder.setCode(rs.getString("Code"));
					myOrder.setCustomerId(rs.getLong("CustomerId"));
					myOrder.setTotal(rs.getFloat("total"));
					myOrder.setId(id);
					myOrder.setOrderDate(rs.getString("OrderDate"));
					myOrder.setPayMethod(rs.getString("PayMethod"));
					myOrder.setReadyDate(rs.getString("ReadyDate"));
					myOrder.setRecvAddr(rs.getString("RecvAddr"));
					myOrder.setRemark(rs.getString("Remark"));
					myOrder.setRentDays(rs.getInt("RentDays"));
					myOrder.setRecvEmail(rs.getString("RecvEmail"));
					myOrder.setRecvName(rs.getString("RecvName"));
					myOrder.setRecvPhone(rs.getString("RecvPhone"));
					myOrder.setRecvProvince(rs.getString("RecvProvince"));
					myOrder.setPostCode(rs.getString("PostCode"));
					myOrder.setPostCost(rs.getInt("PostCost"));
					myOrder.setPostMethod(rs.getString("PostMethod"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return myOrder;
	}
	/**
	 * 
	 * @param id
	 * @return
	 */
	public MyOrder getMyOrder(String code)
	{
		MyOrder myOrder=null;
		
		String sql="select * from myorder where Code=?";
		Object[] args={code};
		ResultSet rs=db.preQuery(sql, args);
		
		if(rs!=null)
		{
			try {
				if(rs.next())
				{
					myOrder=new MyOrder();
					
					myOrder.setCode(code);
					myOrder.setCustomerId(rs.getLong("CustomerId"));
					myOrder.setTotal(rs.getFloat("total"));
					myOrder.setId(rs.getLong("Id"));
					myOrder.setOrderDate(rs.getString("OrderDate"));
					myOrder.setPayMethod(rs.getString("PayMethod"));
					myOrder.setReadyDate(rs.getString("ReadyDate"));
					myOrder.setRecvAddr(rs.getString("RecvAddr"));
					myOrder.setRemark(rs.getString("Remark"));
					myOrder.setRentDays(rs.getInt("RentDays"));
					myOrder.setRecvEmail(rs.getString("RecvEmail"));
					myOrder.setRecvName(rs.getString("RecvName"));
					myOrder.setRecvPhone(rs.getString("RecvPhone"));
					myOrder.setRecvProvince(rs.getString("RecvProvince"));
					myOrder.setPostCode(rs.getString("PostCode"));
					myOrder.setPostCost(rs.getInt("PostCost"));
					myOrder.setPostMethod(rs.getString("PostMethod"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return myOrder;
	}
	/**
	 * 
	 * @param myOrder
	 * @return
	 */
	public boolean addMyOrder(MyOrder myOrder)
	{
		boolean flag=false;
		String sql="insert into myorder(Code,CustomerId,total,OrderDate,PayMethod,ReadyDate,RecvAddr,Remark,RentDays,RecvName,RecvProvince,RecvPhone,RecvEmail,PostCode,PostMethod,PostCost) ";
		sql+=" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		
		Object[] args={myOrder.getCode(),
				myOrder.getCustomerId(),
				myOrder.getTotal(),
				myOrder.getOrderDate(),
				myOrder.getPayMethod(),
				myOrder.getReadyDate(),
				myOrder.getRecvAddr(),
				myOrder.getRemark(),
				myOrder.getRentDays(),
				myOrder.getRecvName(),
				myOrder.getRecvProvince(),
				myOrder.getRecvPhone(),
				myOrder.getRecvEmail(),
				myOrder.getPostCode(),
				myOrder.getPostMethod(),
				myOrder.getPostCost()};
		
		int count=db.preExecute(sql, args);
		
		flag=count>0;
		
		return flag;
	}
	/**
	 * 
	 * @param myOrder
	 * @return
	 */
	public boolean updateMyOrder(MyOrder myOrder)
	{
		boolean flag=false;
		String sql="update myorder set ";
		sql+=" Code=?,";
		sql+=" CustomerId=?,";
		sql+=" total=?,";
		sql+=" OrderDate=?,";
		sql+=" PayMethod=?,";
		sql+=" ReadyDate=?,";
		sql+=" RecvAddr=?,";
		sql+=" Remark=?,";
		sql+=" RentDays=?,";
		sql+=" RecvName=?,";
		sql+=" RecvProvince=?,";
		sql+=" RecvPhone=?,";
		sql+=" RecvEmail=?,";
		sql+=" PostCode=?,";
		sql+=" PostMethod=?,";
		sql+=" PostCost=?";
		sql+=" where Id=?";
		
		Object[] args={myOrder.getCode(),
				myOrder.getCustomerId(),
				myOrder.getTotal(),
				myOrder.getOrderDate(),
				myOrder.getPayMethod(),
				myOrder.getReadyDate(),
				myOrder.getRecvAddr(),
				myOrder.getRemark(),
				myOrder.getRentDays(),
				myOrder.getRecvName(),
				myOrder.getRecvProvince(),
				myOrder.getRecvPhone(),
				myOrder.getRecvEmail(),
				myOrder.getPostCode(),
				myOrder.getPostMethod(),
				myOrder.getPostCost(),
				myOrder.getId()};
		
		int count=db.preExecute(sql, args);
		
		flag=count>0;
		
		return flag;
	}
	/**
	 * 
	 * @param id
	 * @return
	 */
	public boolean deleteMyOrder(long id)
	{
		boolean flag=false;
		String sql="delete from myorder where Id=?";
		
		Object[] args={id};
		
		int count=db.preExecute(sql, args);
		
		flag=count>0;
		
		return flag;
	}

	/**
	 * 
	 * @param start
	 * @param limit
	 * @param condition
	 * @return
	 */
	public List<MyOrder> getOrders(int start,int limit,String condition)
	{
		List<MyOrder> list=null;
		String sql="select * from myorder";
		if(!condition.equals(""))
		{
			sql+=" where "+condition;
		}
		
		sql+=" order by OrderDate desc ";
		sql+=" limit ?,?";
		
		Object[] args={start,limit};
		ResultSet rs=db.preQuery(sql, args);
		
		if(rs!=null)
		{
			list=new ArrayList<MyOrder>();
			try {
				while(rs.next())
				{
					MyOrder myOrder=new MyOrder();
					myOrder.setCode(rs.getString("Code"));
					myOrder.setCustomerId(rs.getLong("CustomerId"));
					myOrder.setTotal(rs.getFloat("total"));
					myOrder.setId(rs.getLong("Id"));
					myOrder.setOrderDate(rs.getString("OrderDate"));
					myOrder.setPayMethod(rs.getString("PayMethod"));
					myOrder.setReadyDate(rs.getString("ReadyDate"));
					myOrder.setRecvAddr(rs.getString("RecvAddr"));
					myOrder.setRemark(rs.getString("Remark"));
					myOrder.setRentDays(rs.getInt("RentDays"));
					myOrder.setRecvEmail(rs.getString("RecvEmail"));
					myOrder.setRecvName(rs.getString("RecvName"));
					myOrder.setRecvPhone(rs.getString("RecvPhone"));
					myOrder.setRecvProvince(rs.getString("RecvProvince"));
					myOrder.setPostCode(rs.getString("PostCode"));
					myOrder.setPostCost(rs.getInt("PostCost"));
					myOrder.setPostMethod(rs.getString("PostMethod"));
					
					list.add(myOrder);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return list;
	}

	/**
	 * 
	 * @param condition
	 * @return
	 */
	public int getCount(String condition)
	{
		int count=0;
		String sql="select count(*) from myorder";
		if(!condition.equals(""))
		{
			sql+=" where "+condition;
		}
		
		ResultSet rs=db.preQuery(sql, null);
		if(rs!=null)
		{
			try {
				if(rs.next())
				{
					count=rs.getInt(1);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return count;
	}
}
